﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Xiaowei.Models;
using Xiaowei.Services;

namespace Xiaowei.Controls
{
    /// <summary>
    /// XiaoweiPanel.xaml 的交互逻辑
    /// </summary>
    public partial class XiaoweiPanel : UserControl
    {
        DispatcherTimer ResetChatListTimer = new DispatcherTimer();
        public static DependencyProperty XiaoweiAIStateProperty = DependencyProperty.Register(
        "XiaoweiAIState", typeof(XiaoweiAI.XiaoweiAIStateEnum), typeof(XiaoweiPanel),
        new PropertyMetadata(XiaoweiAI.XiaoweiAIStateEnum.Idle, (s, e) => {
            XiaoweiPanel xiaoweiPanel = s as XiaoweiPanel;
            switch ((XiaoweiAI.XiaoweiAIStateEnum)e.NewValue)
            {
                case XiaoweiAI.XiaoweiAIStateEnum.Idle:
                    xiaoweiPanel.activeButton.SwitchStateToNormal();
                    break;
                case XiaoweiAI.XiaoweiAIStateEnum.ListeningPrepare:
                    xiaoweiPanel.activeButton.SwitchStateToListening();
                    break;
                case XiaoweiAI.XiaoweiAIStateEnum.Listening:
                    xiaoweiPanel.activeButton.SwitchStateToListening();
                    break;
                case XiaoweiAI.XiaoweiAIStateEnum.Thinking:
                    xiaoweiPanel.activeButton.SwitchStateToListening();
                    break;
                case XiaoweiAI.XiaoweiAIStateEnum.Speaking:
                    xiaoweiPanel.activeButton.SwitchStateToNormal();
                    break;
                case XiaoweiAI.XiaoweiAIStateEnum.Mute:
                    break;
                default:
                    break;
            }
            if((XiaoweiAI.XiaoweiAIStateEnum)e.NewValue== XiaoweiAI.XiaoweiAIStateEnum.Idle|| (XiaoweiAI.XiaoweiAIStateEnum) e.NewValue == XiaoweiAI.XiaoweiAIStateEnum.Mute)
            {
                if (!xiaoweiPanel.isMouseEnter)
                {
                    xiaoweiPanel.Dispatcher.Invoke(() => { xiaoweiPanel.ResetChatListTimer.Start(); });
                }
            }
            else
            {
                xiaoweiPanel.Dispatcher.Invoke(()=> { xiaoweiPanel.ResetChatListTimer.Stop(); });
            }
        })
       );
        public XiaoweiAI.XiaoweiAIStateEnum XiaoweiAIState
        {
            get
            {
                return (XiaoweiAI.XiaoweiAIStateEnum)GetValue(XiaoweiAIStateProperty);
            }
            set
            {
                SetValue(XiaoweiAIStateProperty, value);
            }
        }
        public XiaoweiPanel()
        {
            InitializeComponent();
            ResetChatListTimer.Tick += ResetChatListTimer_Tick;
            ResetChatListTimer.Interval = TimeSpan.FromSeconds(10);
        }

        private void ResetChatListTimer_Tick(object sender, EventArgs e)
        {
            XiaoweiAI.Instance.ClearHistory();
        }

        public void ShowLastItem()
        {
            Dispatcher.Invoke(()=> {
                chatList.ScrollIntoView(chatList.Items[chatList.Items.Count-1]);
            });
        }

        bool isMoveCommand = false;

        Point lastMousePosition = new Point();

        private void activeButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isMoveCommand = false;
            lastMousePosition = PointToScreen(e.GetPosition(this));
            activeButton.CaptureMouse();
        }

        private void activeButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            activeButton.ReleaseMouseCapture();
            if(!isMoveCommand)
            {
                if (XiaoweiAIState == XiaoweiAI.XiaoweiAIStateEnum.Mute)
                {
                    return;
                }
                if (XiaoweiAIState == XiaoweiAI.XiaoweiAIStateEnum.Listening)
                {
                    _ = ApplicationModel.Instance.AbortActivation();
                }
                else
                {
                    ApplicationModel.Instance.Activate(false, ActivateType.PushToTalk);
                }
            }

        }

        private void activeButton_MouseMove(object sender, MouseEventArgs e)
        {
            if (activeButton.IsMouseCaptured)
            {
                
                Point currentMousePosition = PointToScreen(e.GetPosition(this));
                if (currentMousePosition != lastMousePosition)
                {
                    isMoveCommand = true;
                    var left = Canvas.GetLeft(this);
                    var top = Canvas.GetTop(this);
                    Canvas.SetLeft(this, left + currentMousePosition.X - lastMousePosition.X);
                    Canvas.SetTop(this, top + currentMousePosition.Y - lastMousePosition.Y);
                    lastMousePosition = currentMousePosition;
                }
                
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(this).Close();
        }

        private void ShowMainWindowButton_Click(object sender, RoutedEventArgs e)
        {

            Task.Run(()=> {
                ServiceFactory.OpenAppService.OpenApp("惠小微");
            });

        }

        bool isMouseEnter = false;

        private void XiaoweiPanel_MouseEnter(object sender, MouseEventArgs e)
        {
            isMouseEnter = true;
            if(chatList.Items.Count!=0)
            {
                panelBorder.Visibility = Visibility.Visible;
            }
        }

        private void XiaoweiPanel_MouseLeave(object sender, MouseEventArgs e)
        {
            isMouseEnter = false;
            if (XiaoweiAIState == XiaoweiAI.XiaoweiAIStateEnum.Idle || XiaoweiAIState == XiaoweiAI.XiaoweiAIStateEnum.Mute)
            {
                ResetChatListTimer.Start();
            }
            panelBorder.Visibility = Visibility.Hidden;
        }
    }
}
